﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DAL = AccessDAL;
using CommonFunc;

public partial class Manageweb_ManageNews_AddNews : System.Web.UI.Page
{
    DAL.NewsClassTableAdapters.NewsClassTableAdapter DNC =
        new DAL.NewsClassTableAdapters.NewsClassTableAdapter();

    MODEL.News MN = new MODEL.News();
    MODEL.News _MN = new MODEL.News();
    MODEL.NewsClass MNC = new MODEL.NewsClass();
    MODEL.NewsClass _MNC = new MODEL.NewsClass();

    BLL.News BN = new BLL.News();
    BLL.NewsClass BNC = new BLL.NewsClass();

    protected delegate string UploadThumb(FileUpload _FU, int _ThumbHeight, string _Prifix);

    protected delegate void JumpPage(string _PageName, Page _Page);
    JumpPage JP = new JumpPage(CommonFunction.JumpAnotherPage);

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.Cookies["MasterID"] == null)
        {
            Response.Redirect("../Default.aspx");
        }

        if (!IsPostBack)
        {
            DDL_ClassDataBind();

            if (Request.QueryString["NewsID"] != null)
            {
                LB_ID.Text = Request.QueryString["NewsID"].ToString();
                PageDataInit();
            }

            if (Request.QueryString["ClassID"] != null)
            {
                DDL_Class.SelectedValue = Request.QueryString["ClassID"].ToString();
            }
        }

        ClickInit();
    }


    #region ClickInit [初始化]
    protected void ClickInit()
    {
        try
        {
            LB_AddNews.Text = "";

            if (LB_ID.Text == "")
            {
                Btn_Submit.Click += new EventHandler(Btn_Add_Click);
                LB_TableTitle.Text = "添加文章";

            }
            else
            {
                Btn_Submit.Click += new EventHandler(Btn_Mod_Click);
                LB_TableTitle.Text = "修改文章";
            }
        }
        catch (Exception ex)
        {
            LB_AddNews.Text = ex.Message;
        }
    }
    #endregion


    #region 页面数据 [初始化]
    protected void PageDataInit()
    {
        try
        {
            MN.News_ID = Convert.ToInt32(LB_ID.Text);

            _MN = BN.GetDataByID(MN);

            TB_Title.Text = _MN.News_Title;
            DDL_Class.SelectedValue = _MN.News_ClassID.ToString();
            CB_IsLock.Checked = _MN.News_IsLock;
            FTB_Content.Text = _MN.News_Content;
        }
        catch (Exception ex)
        {
            LB_AddNews.Text = ex.Message;
        }
    }
    #endregion


    #region DDL_Class数据绑定 [初始化]
    protected void DDL_ClassDataBind()
    {
        try
        {
            DDL_Class.Items.Clear();

            ListItem LI0 = new ListItem("", "");
            DDL_Class.Items.Add(LI0);

            DataTable DT = DNC.GetAllDataOrderByClassCode();

            foreach (DataRow dr in DT.Rows)
            {
                ListItem li = new ListItem(
                    FormatClassTitle(dr["NC_Title"], dr["NC_Level"]), dr["NC_ID"].ToString());
                DDL_Class.Items.Add(li);
            }
        }
        catch (Exception ex)
        {
            LB_AddNews.Text = ex.Message;
        }
    }
    #endregion


    #region 类别标题 [格式化]
    protected string FormatClassTitle(object _Title, object _Level)
    {
        return ClassCode.CreateClassListItemText(_Title.ToString(), _Level.ToString());
    }
    #endregion


    #region 添加文章 [Click]
    protected void Btn_Add_Click(object sender, EventArgs e)
    {
        try
        {
            MN.News_Title = TB_Title.Text;
            MN.News_ClassID = Convert.ToInt32(DDL_Class.SelectedValue);
            MN.News_Content = FTB_Content.Text;
            MN.News_DateTime = DateTime.Now;
            MN.News_Hits = 0;
            MN.News_MasterID = Convert.ToInt32(Session["MasterID"]);
            MN.News_IsLock = CB_IsLock.Checked;
            MN.News_IsDel = false;

            BN.Add(MN);

            LB_AddNews.Text = "添加成功，页面即将自动跳转";

            MNC.NC_ID = MN.News_ClassID;

            _MNC = BNC.GetDataByID(MNC);

            if (_MNC.NC_IsLock)
            {
                JP("ManageNews.aspx?ClassID=" + _MNC.NC_ID, this.Page);
            }
            else
            {
                JP("ManageNews.aspx", this.Page); 
            }
        }
        catch (Exception ex)
        {
            LB_AddNews.Text = ex.Message;
        }
    }
    #endregion


    #region 修改文章 [Click]
    protected void Btn_Mod_Click(object sender, EventArgs e)
    {
        try
        {
            MN.News_Title = TB_Title.Text;
            MN.News_ClassID = Convert.ToInt32(DDL_Class.SelectedValue);
            MN.News_Content = FTB_Content.Text;
            MN.News_IsLock = CB_IsLock.Checked;
            MN.News_ID = Convert.ToInt32(LB_ID.Text);

            BN.Mod(MN);

            LB_AddNews.Text = "修改成功，页面即将自动跳转";

            JP("ManageNews.aspx", this.Page);
        }
        catch (Exception ex)
        {
            LB_AddNews.Text = ex.Message;
        }
    }
    #endregion
}
